<?php
class PaymentBuyItNow extends PaymentOrderInfo {
	
	protected $bidType=1;
	protected $auctionInfo;
	protected $auctionId;
	public $customerId;
	
	public function __construct(){
		parent::__construct();
		$this->setName('buyItNow');
		$this->_orderType = ConstService::ORDER_TYPE_BUY_IT_NOW;
	}
	
	public function setBidType($bidType){
		$this->bidType = $bidType;
		return $this;
	}
	
	public function setAuctionInfo($auctionInfo){
		$this->auctionInfo = $auctionInfo;
		return $this;
	}
	
	public function setCustomerId($customerId){
		$this->customerId = $customerId;
		return $this;
	}
	
	public function setAuctionId($auctionId){
		$this->auctionId = $auctionId;
		return $this;
	}

	public function setOrderInfoFromAuction($customerId,$orderId=null){
		$this->_orderAr = new Orders();
		if(!empty($orderId)){
			$this->_orderAr->id = $orderId;
		}
// 		$this->_orderAr->unsetAttributes();
		$time = time();
		$this->_orderAr->auction_id = $this->auctionInfo->id;
		$this->_orderAr->date_won = $time;
		
		$this->_orderAr->customer_id = $customerId;
		
		$this->_orderAr->price = $this->buyItNowPrice();
		$this->_orderAr->vendor_id=$this->auctionInfo->vendor;
		$this->_orderAr->quantity = $this->auctionInfo->number;
		$this->_orderAr->total_price = ($this->buyItNowPrice()+$this->auctionInfo->cost+$this->auctionInfo->handling_cost);
		$this->_orderAr->status = 1;
		$this->_orderAr->type = $this->_orderType;
		$this->_orderAr->created = $time;
		$this->_orderAr->shipping_fee = $this->auctionInfo->cost;
		$this->_orderAr->shipping_handling = $this->auctionInfo->handling_cost;
		$this->_orderAr->auction_sku = $this->auctionInfo->auction_sku;
		$this->_orderAr->tax_fee = 0;
		return $this;
	}
	
	public function saveInfo(){
		if($this->_orderAr->id){
			$orders = new Orders();
			$q = $orders->updateByPk($this->_orderAr->id, $this->_orderAr->attributes);
			$orders->afterUpdate($this->_orderAr);
			return $q;
		}else{
			$ordersInfo = new Orders();
			foreach ($this->toArray() as $key=>$val){
				if($key !='customerId'){
					$ordersInfo->$key = $val;
				}
			}
			$ordersInfo->customer_id = $this->_orderAr->customer_id;
				
	
			$ordersInfo->save();
			$this->_orderAr->id = $ordersInfo->id;
			return $ordersInfo->id;
		}
	
	}

	public function buyItNowPrice(){

		$buyItNowPrice = 0;
		$pararray = array(
				':customer_id'=>$this->customerId,
				':auctions_id'=>$this->auctionId,
				':bid_type'		=> 1,
		);

		$bidnum = CustomerBidAuctionCount::model()->findByAttributes(array('customer_id'=>yii::app()->user->id,'auction_id'=>$this->auctionId));
		$bidCount = 0;

		if(count($bidnum)){
			$bidCount = $bidnum->bid_num;
		}
		
		$binPrice =  $this->auctionInfo->value_price-$bidCount*ConstService::BID_VALUE_BUY_IT_NOW;
		return ($binPrice > 0 )?number_format($binPrice, 2, '.', ''):0;		
	}
	
	public function orderExistsVaild(){
		$orderBuyIntNow = Orders::model()->count(' customer_id=:customerId and (  (type = '.ConstService::ORDER_TYPE_BUY_IT_NOW.' and status in ('.ConstService::ORDER_UNPAID.') ) or ( type = '.ConstService::ORDER_TYPE_BUY_AUCTION.')   )  and auction_id = :auctionId',array(
				':customerId'	=> $this->customerId,
				':auctionId'	=> $this->auctionId,
		));
		return $orderBuyIntNow > 0 ;
	}
	
	public function orderVaild(){
		$orderBuyIntNow = Orders::model()->count(' customer_id=:customerId and (  (type = '.ConstService::ORDER_TYPE_BUY_IT_NOW.' and status in ('.ConstService::ORDER_PROCESSING_BUT_NOT_DELIVERED.','.ConstService::ORDER_DELIVERED.') ) or ( type = '.ConstService::ORDER_TYPE_BUY_AUCTION.')   )  and auction_id = :auctionId',array(
				':customerId'	=> $this->customerId,
				':auctionId'	=> $this->auctionId,
		));
		return $orderBuyIntNow > 0 ;
	}
	
	public function vaild(){
		
		if ($this->orderVaild()){
			$this->setErrorMessage('buyItNowExists','buy it now exists');
		}
		if (empty($this->customerId)){
			$this->setErrorMessage('customerId','customerId NO');
		}
		
		switch ($this->scene){
			case 'binexpire':
				// if (time()-$this->auctionInfo->expires_time>=$this->auctionInfo->buyitnow_expired_time){
				// 	$this->setErrorMessage('auction order ','auction BIN Order Expired');
				// 	return $this->vaild;
				// }
				break;
			case 'show':
		
				break;
		}
		return $this->vaild;
	}
	
	public function toArray(){
		$toArray = array();
		if(!empty($this->_orderAr)){
			$toArray = $this->_orderAr->attributes;
			$toArray['customerId'] = $this->customerId;
		}
		
		return $toArray;
	}
	
}